package com.jhd.fuzhou.view.special;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.vo.QA;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;
import static com.jhd.fuzhou.view.login.MainActivity.qaList;

public class SpecialAskingListActivity extends BaseActivity {
    @BindView(R.id.btn_add)
    Button btn_add;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private Context context= this;
    private AskingContentAdapter askingContentAdapter;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_asking_list);
    }

    @Override
    protected void processLogic() {
        layoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        if(qaList.size()==0){
            startActivity(new Intent(context,SpecialAskingQuestionActivity.class));
        }else{
            askingContentAdapter=new AskingContentAdapter(qaList);
            recyclerView.setAdapter(askingContentAdapter);
        }
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        askingContentAdapter=new AskingContentAdapter(qaList);
        recyclerView.setAdapter(askingContentAdapter);
    }

    @OnClick({R.id.btn_add})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.btn_add:
                startActivity(new Intent(context, SpecialAskingQuestionActivity.class));
                break;
        }
    }

    public class AskingContentAdapter extends RecyclerView.Adapter<AskingContentAdapter.ViewHolder> {
        private List<QA> qaList;

        public AskingContentAdapter(List<QA> qaList) {
            this.qaList = qaList;
        }

        @NonNull
        @Override
        public AskingContentAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_question_answer, parent, false);
            AskingContentAdapter.ViewHolder viewHolder = new AskingContentAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull AskingContentAdapter.ViewHolder holder, int position) {
            holder.tv_asking_num.setText("询问内容("+(position+1)+")");
            holder.tv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    qaList.remove(position);
                    notifyDataSetChanged();
                }
            });
            holder.tv_question.setText(qaList.get(position).getQuestion());
            holder.tv_answer.setText(qaList.get(position).getAnswer());


        }

        @Override
        public int getItemCount() {
            return qaList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_asking_num,tv_delete,tv_question,tv_answer;
            public ViewHolder(View itemView) {
                super(itemView);
                this.tv_asking_num = itemView.findViewById(R.id.tv_asking_num);
                this.tv_delete = itemView.findViewById(R.id.tv_delete);
                this.tv_question = itemView.findViewById(R.id.tv_question);
                this.tv_answer = itemView.findViewById(R.id.tv_answer);
            }
        }
    }
}